package com.qfhd.soft.cjzcore;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;

import com.qfhd.soft.cjzcore.Core.CJDevice;
import com.qfhd.soft.cjzcore.Core.CJFile;
import com.qfhd.soft.cjzcore.Core.CJHttp;
import com.qfhd.soft.cjzcore.Core.CJJsonUtils;
import com.qfhd.soft.cjzcore.Core.CJLogger;
import com.qfhd.soft.cjzcore.Core.CJPermission;
import com.qfhd.soft.cjzcore.TaskCenter.CJAdCenter;
import com.qfhd.soft.cjzcore.User.CJZUser;
import com.qfhd.soft.cjzcore.User.CJZUserHelper;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.MultipartBody;

public class CJZManager {
    private boolean isInit = false;//初始化成功与否

    private static CJZManager manager;

    public static CJZManager getInstance() {
        if (manager == null) {
            synchronized (CJZManager.class) {
                manager = new CJZManager();
            }
        }
        return manager;
    }

    /**
     * @param gdt_appid 广点通 appid
     * @param csj_appid 穿山甲 appid
     */
    private void getAppId(String gdt_appid, String csj_appid) {
        CJAdCenter.getInstance().setAPPID_GDT(gdt_appid);
        CJAdCenter.getInstance().setAPPID_CSJ(csj_appid);
    }

    public void applicatonInit(Context context, String gdt_appid, String csj_appid) {
        //初始化广告

        //初始化广告前 要先获取广告需要的 key
        getAppId(gdt_appid, csj_appid);

        if (CJAdCenter.getInstance().appIdIsGetter()) {
            CJAdCenter.getInstance().initAd(context);
        } else {
            CJLogger.v(" gdt or csj appid is empty");
        }
    }

    public boolean initData(Context context, Activity activity, String appKey) {
        //APPKEY
        //获取token
        //权限

        CJLogger.v("initData");
        if (TextUtils.isEmpty(appKey)) {
            CJLogger.v("APPKEY is emptry");
            return false;
        } else {
            CJZConstant.APPKEY = appKey;
        }
        CJFile.getInstance().init(context);
        CJDevice.getInstance().init(context);
        if (Build.VERSION.SDK_INT >= 23) {
            boolean ret = CJPermission.checkAndRequestPermission(context, activity,null);
            if (ret) {
                //已有权限
                return getToken();
            } else {
                //没有权限
                CJLogger.v("permission have not");
                return false;
            }
        } else {
            // 如果是Android6.0以下的机器，建议在manifest中配置相关权限，这里可以直接调用SDK
            ///todo
            CJLogger.v("android < 23");
            return getToken();
        }
    }

    private boolean getToken() {
        testGetAdInfos();
        return CJZUser.getInstance().getInfo(CJZUserHelper.getRequest_sigle(CJZConstant.API.fuc_deviceLogin), CJZUserHelper.getParamas(CJDevice.getInstance().getDeviceJson()));
    }

    private void testGetAdInfos() {
        JSONObject obj1 = getJSONObjson(CJAdCenter.AD_Place_screen, CJAdCenter.Type_Splash, 3, "4031305349230215", 4, "838180942");
        JSONObject obj2 = getJSONObjson(CJAdCenter.AD_Place_ggk_video, CJAdCenter.Type_Video, 2, "3081100369038475", 2, "938180431");
        JSONObject obj3 = getJSONObjson(CJAdCenter.AD_Place_ggk_navi, CJAdCenter.Type_Navi, 2, "9041209329337360", 2, "938180884");
        JSONObject obj4 = getJSONObjson(CJAdCenter.AD_Place_ggk_Dialoig_navi, CJAdCenter.Type_Navi, 2, "9041209329337360", 2, "938180884");
        JSONObject obj5 = getJSONObjson(CJAdCenter.AD_Place_list, CJAdCenter.Type_Navi, 2, "9041209329337360", 2, "938180884");
        JSONArray array = new JSONArray();
        array.put(obj1);
        array.put(obj2);
        array.put(obj3);
        array.put(obj4);

        CJAdCenter.getInstance().setAdInfos(array);
    }

    private JSONObject getJSONObjson(int ggPlace, int ggStyle, int gdt_weight, String gdt_appid, int csj_weight, String csj_appid) {
        JSONObject obj1 = new JSONObject();
        try {
            obj1.put("ggPlace", ggPlace);
            obj1.put("ggStyle", ggStyle);

            JSONArray array1 = new JSONArray();
            JSONObject obj11 = new JSONObject();
            obj11.put("ggResource", 4402);
            obj11.put("weight", gdt_weight);
            obj11.put("ggID", gdt_appid);
            JSONObject obj12 = new JSONObject();
            obj12.put("ggResource", 4401);
            obj12.put("weight", csj_weight);
            obj12.put("ggID", csj_appid);
            array1.put(obj11);
            array1.put(obj12);

            obj1.put("ggWeight",array1);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return obj1;
    }

    public boolean getMainAcivityData(){
        ArrayList<String> requestArray = new ArrayList<>();
        requestArray.add(CJZConstant.API.fuc_userInfo);
        requestArray.add(CJZConstant.API.fuc_ggkList);
        requestArray.add(CJZConstant.API.fuc_tryPlace);
        requestArray.add(CJZConstant.API.fuc_imgTaskList);

        String paramas;
        JSONObject tmp1 = CJZUserHelper.getParamasJSONObject_default();
        return CJZUser.getInstance().getInfo(CJZUserHelper.getRequest_mutil(requestArray),tmp1.toString());
    }

    public boolean sign(){
        ArrayList<String> requestArray = new ArrayList<>();
        requestArray.add(CJZConstant.API.fuc_sign);

        String paramas;
        JSONObject tmp1 = CJZUserHelper.getParamasJSONObject_default();
        return CJZUser.getInstance().getInfo(CJZUserHelper.getRequest_mutil(requestArray),tmp1.toString());
    }

    public boolean ggkDetail(String sn){
        ArrayList<String> requestArray = new ArrayList<>();
        requestArray.add(CJZConstant.API.fuc_ggkDetail);

        String paramas;
        JSONObject tmp1 = CJZUserHelper.getParamasJSONObject_default();
        try {
            tmp1.put("cards_sn",sn);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return CJZUser.getInstance().getInfo(CJZUserHelper.getRequest_mutil(requestArray),tmp1.toString());
    }

    public boolean ggkReport(String sn){
        ArrayList<String> requestArray = new ArrayList<>();
        requestArray.add(CJZConstant.API.fuc_ggkReport);

        String paramas;
        JSONObject tmp1 = CJZUserHelper.getParamasJSONObject_default();
        try {
            tmp1.put("cards_sn",sn);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return CJZUser.getInstance().getInfo(CJZUserHelper.getRequest_mutil(requestArray),tmp1.toString());
    }

    public boolean taskImgGetDetail(String tid){
        ArrayList<String> requestArray = new ArrayList<>();
        requestArray.add(CJZConstant.API.fuc_imgTaskDetail);

        String paramas;
        JSONObject tmp1 = CJZUserHelper.getParamasJSONObject_default();
        try {
            tmp1.put("tid",tid);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return CJZUser.getInstance().getInfo(CJZUserHelper.getRequest_mutil(requestArray),tmp1.toString());
    }

    public boolean taskImgSubmit(String tid,int taskId,String result){
        ArrayList<String> requestArray = new ArrayList<>();
        requestArray.add(CJZConstant.API.fuc_imgTaskSubmit);

        String paramas;
        JSONObject tmp1 = CJZUserHelper.getParamasJSONObject_default();
        try {
            tmp1.put("tid",tid);
            tmp1.put("task_id",taskId);
            tmp1.put("result",result);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return CJZUser.getInstance().getInfo(CJZUserHelper.getRequest_mutil(requestArray),tmp1.toString());
    }

    public String uploadImg(List<String> files, int taskId, int submitId) {
        String ret = "";
        JSONObject request = new JSONObject();
        try {
            MultipartBody.Builder builder = new MultipartBody.Builder();
            builder.setType(MultipartBody.FORM);
            builder.addFormDataPart("appkey", CJZConstant.APPKEY);
            builder.addFormDataPart("token", CJZUser.getInstance().getToken());
            builder.addFormDataPart("task_id",taskId+"");
            builder.addFormDataPart("require_id",submitId+"");
            CJLogger.v("uploadFile resonse taskId：" + taskId + " require_id:" + submitId);
            String response = CJHttp.getInstance().post_uploadImg(CJZConstant.API.fuc_imgTaskUpLoadImg,files,builder);
            CJLogger.v("uploadFile resonse :" + response);
            JSONObject dataObj = CJZUser.getInstance().responMethod(response,"uploadFile");
            if (dataObj != null) {
                if(dataObj.has("img_url")){
                    ret = dataObj.getString("img_url");
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
            CJLogger.v("uploadImg error:" + e.toString());
        }
        return ret;
    }

    public boolean tryPlaceGetTask(String tid){
        ArrayList<String> requestArray = new ArrayList<>();
        requestArray.add(CJZConstant.API.fuc_tryPlace_getTask);

        String paramas;
        JSONObject tmp1 = CJZUserHelper.getParamasJSONObject_default();
        try {
            tmp1.put("tid",tid);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return CJZUser.getInstance().getInfo(CJZUserHelper.getRequest_mutil(requestArray),tmp1.toString());
    }

    public boolean tryPlaceGetDetail(String tid){
        ArrayList<String> requestArray = new ArrayList<>();
        requestArray.add(CJZConstant.API.fuc_tryPlaceDetail);

        String paramas;
        JSONObject tmp1 = CJZUserHelper.getParamasJSONObject_default();
        try {
            tmp1.put("tid",tid);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return CJZUser.getInstance().getInfo(CJZUserHelper.getRequest_mutil(requestArray),tmp1.toString());
    }

    public boolean tryPlaceReport(String tid,int taskId,int sectionId){
        ArrayList<String> requestArray = new ArrayList<>();
        requestArray.add(CJZConstant.API.getFuc_tryPlace_submit);

        String paramas;
        JSONObject tmp1 = CJZUserHelper.getParamasJSONObject_default();
        try {
            tmp1.put("tid",tid);
            tmp1.put("task_id",taskId);
            tmp1.put("section_id",sectionId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return CJZUser.getInstance().getInfo(CJZUserHelper.getRequest_mutil(requestArray),tmp1.toString());
    }
}
